<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Configuration;

use ArrayObject;
use SplObjectStorage;

/**
 * Base implementation of ContextInterface
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class BaseContext implements ContextInterface
{
	/**
	 * @var ArrayObject<string:ContextInterface>
	 */
	protected $children;
	
	/**
	 * @var string
	 */
	protected $identifier;
	
	/**
	 * @var ContextInterface
	 */
	protected $parentContext;
	
	/**
	 * @var string
	 */
	protected $path;
	
	/**
	 * @var SplObjectStorage<PolicyInterface>
	 */
	protected $policies;
	
	/**
	 * Class constructor
	 *
	 * @param string $identifier [optional]
	 * @param ContextInterface $parentContext [optional]
	 * @return void
	 */
	public function __construct($identifier, ContextInterface $parentContext = null)
	{
		$this->identifier = $identifier;
		$this->parentContext = $parentContext;
		
		if(!is_null($parentContext)) {
			$this->path = sprintf('%s:%s', $parentContext->getPath(), $identifier);
		} else {
			$this->path = '';
		}
		
		$this->children = new ArrayObject();
		$this->policies = new SplObjectStorage();
	}
	
	/**
	 * Binds a policy to this context
	 *
	 * @param PolicyInterface $policy
	 * @return void
	 */
	public function bind(PolicyInterface $policy)
	{
		$this->policies->attach($policy);
	}
	
	/**
	 * Unbinds a policy from this context
	 *
	 * @param PolicyInterface $policy
	 * @return void
	 */
	public function unbind(PolicyInterface $policy)
	{
		$this->policies->detach($policy);
	}
	
	/**
	 * Returns a child context with a given identifier
	 *
	 * @param string $identifier
	 * @return ContextInterface
	 */
	public function getChildContext($identifier)
	{
		if(!$this->children->offsetExists($identifier)) {
			$this->children->offsetSet($identifier, new static($identifier, $this));
		}
		return $this->children->offsetGet($identifier);
	}
	
	/**
	 * Returns the children contexts of this context
	 * @return ArrayObject<ContextInterface>
	 */
	public function getChildren()
	{
		return $this->children;
	}
	
	/**
	 * Returns the identifier that this context affects
	 * @return string
	 */
	public function getIdentifier()
	{
		return $this->identifier;
	}
	
	/**
	 * Returns parent context of this context (if any)
	 * @return ContextInterface
	 */
	public function getParentContext()
	{
		return $this->parentContext;
	}
	
	/**
	 * Returns the path to this context
	 * @return string
	 */
	public function getPath()
	{
		return $this->path;
	}
	
	/**
	 * Returns a set of the policies bound to this context
	 * @return SplObjectStorage<PolicyInterface>
	 */
	public function getPolicies()
	{
		return $this->policies;
	}
	
	/**
	 * Returns the number of policies bound to this context
	 * @return int
	 */
	public function count()
	{
		return $this->policies->count();
	}
	
	/**
	 * Returns an iterator that can be used to find the root context
	 * @return Iterator
	 */
	public function getIterator()
	{
		return new ContextIterator($this);
	}
}